/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using QuocTeViet.DO;
/// <summary>
/// Summary description for cmsDonHangDAL
/// </summary>
namespace QuocTeViet.DAL
{

    public class cmsDonHangDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public cmsDonHangDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(cmsDonHangDO objcmsDonHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@HoTen", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.HoTen;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@DiaChi", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.DiaChi;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SDT", SqlDbType.VarChar);
            Sqlparam.Value = objcmsDonHangDO.SDT;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Email", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.Email;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@HTTT", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.HTTT;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@DiaChiGiaoHang", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.DiaChiGiaoHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TongTien", SqlDbType.Float);
            Sqlparam.Value = objcmsDonHangDO.TongTien;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TienTe", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.TienTe;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@XuLy", SqlDbType.Bit);
            Sqlparam.Value = objcmsDonHangDO.XuLy;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@GhiChu", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.GhiChu;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SessionID", SqlDbType.VarChar);
            Sqlparam.Value = objcmsDonHangDO.SessionID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@UserID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsDonHangDO.UserID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }
        public int Update(cmsDonHangDO objcmsDonHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@DonHangID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsDonHangDO.DonHangID;
            Sqlcomm.Parameters.Add(Sqlparam);


            Sqlparam = new SqlParameter("@HoTen", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.HoTen;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@DiaChi", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.DiaChi;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SDT", SqlDbType.VarChar);
            Sqlparam.Value = objcmsDonHangDO.SDT;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Email", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.Email;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@HTTT", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.HTTT;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@DiaChiGiaoHang", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.DiaChiGiaoHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TongTien", SqlDbType.Float);
            Sqlparam.Value = objcmsDonHangDO.TongTien;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TienTe", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.TienTe;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@XuLy", SqlDbType.Bit);
            Sqlparam.Value = objcmsDonHangDO.XuLy;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@GhiChu", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsDonHangDO.GhiChu;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SessionID", SqlDbType.VarChar);
            Sqlparam.Value = objcmsDonHangDO.SessionID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@UserID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsDonHangDO.UserID;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }
        public int Delete(cmsDonHangDO objcmsDonHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@DonHangID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsDonHangDO.DonHangID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public cmsDonHangDO Select(cmsDonHangDO objcmsDonHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@DonHangID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsDonHangDO.DonHangID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["DonHangID"]))
                    objcmsDonHangDO.DonHangID = Convert.ToInt64(dr["DonHangID"]);
                if (!Convert.IsDBNull(dr["HoTen"]))
                    objcmsDonHangDO.HoTen = Convert.ToString(dr["HoTen"]);
                if (!Convert.IsDBNull(dr["DiaChi"]))
                    objcmsDonHangDO.DiaChi = Convert.ToString(dr["DiaChi"]);
                if (!Convert.IsDBNull(dr["SDT"]))
                    objcmsDonHangDO.SDT = Convert.ToString(dr["SDT"]);
                if (!Convert.IsDBNull(dr["Email"]))
                    objcmsDonHangDO.Email = Convert.ToString(dr["Email"]);
                if (!Convert.IsDBNull(dr["HTTT"]))
                    objcmsDonHangDO.HTTT = Convert.ToString(dr["HTTT"]);
                if (!Convert.IsDBNull(dr["DiaChiGiaoHang"]))
                    objcmsDonHangDO.DiaChiGiaoHang = Convert.ToString(dr["DiaChiGiaoHang"]);
                if (!Convert.IsDBNull(dr["TongTien"]))
                    objcmsDonHangDO.TongTien = Convert.ToDouble(dr["TongTien"]);

                if (!Convert.IsDBNull(dr["TienTe"]))
                    objcmsDonHangDO.TienTe = Convert.ToString(dr["TienTe"]);
                if (!Convert.IsDBNull(dr["XuLy"]))
                    objcmsDonHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);
                if (!Convert.IsDBNull(dr["GhiChu"]))
                    objcmsDonHangDO.GhiChu = Convert.ToString(dr["GhiChu"]);
                if (!Convert.IsDBNull(dr["SessionID"]))
                    objcmsDonHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                if (!Convert.IsDBNull(dr["UserID"]))
                    objcmsDonHangDO.UserID = Convert.ToInt64(dr["UserID"]);

            }
            return objcmsDonHangDO;
        }

        public ArrayList SelectAll1(int xl)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_GetAll";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@xl", SqlDbType.Int);
            Sqlparam.Value = xl;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrcmsDonHangDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cmsDonHangDO objcmsDonHangDO = new cmsDonHangDO();
                    if (!Convert.IsDBNull(dr["DonHangID"]))
                        objcmsDonHangDO.DonHangID = Convert.ToInt64(dr["DonHangID"]);
                    if (!Convert.IsDBNull(dr["HoTen"]))
                        objcmsDonHangDO.HoTen = Convert.ToString(dr["HoTen"]);
                    if (!Convert.IsDBNull(dr["DiaChi"]))
                        objcmsDonHangDO.DiaChi = Convert.ToString(dr["DiaChi"]);
                    if (!Convert.IsDBNull(dr["SDT"]))
                        objcmsDonHangDO.SDT = Convert.ToString(dr["SDT"]);
                    if (!Convert.IsDBNull(dr["Email"]))
                        objcmsDonHangDO.Email = Convert.ToString(dr["Email"]);
                    if (!Convert.IsDBNull(dr["HTTT"]))
                        objcmsDonHangDO.HTTT = Convert.ToString(dr["HTTT"]);
                    if (!Convert.IsDBNull(dr["DiaChiGiaoHang"]))
                        objcmsDonHangDO.DiaChiGiaoHang = Convert.ToString(dr["DiaChiGiaoHang"]);
                    if (!Convert.IsDBNull(dr["TongTien"]))
                        objcmsDonHangDO.TongTien = Convert.ToDouble(dr["TongTien"]);
                    if (!Convert.IsDBNull(dr["TienTe"]))
                        objcmsDonHangDO.TienTe = Convert.ToString(dr["TienTe"]);
                    if (!Convert.IsDBNull(dr["XuLy"]))
                        objcmsDonHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);
                    if (!Convert.IsDBNull(dr["GhiChu"]))
                        objcmsDonHangDO.GhiChu = Convert.ToString(dr["GhiChu"]);
                    if (!Convert.IsDBNull(dr["SessionID"]))
                        objcmsDonHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                    if (!Convert.IsDBNull(dr["UserID"]))
                        objcmsDonHangDO.UserID = Convert.ToInt64(dr["UserID"]);
                    arrcmsDonHangDO.Add(objcmsDonHangDO);
                }
            }
            return arrcmsDonHangDO;
        }

        public DataTable SelectAll(int xl)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_GetAll";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@xl", SqlDbType.Int);
            Sqlparam.Value = xl;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

        public DataTable GetCustomerInfo(string session)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_GetCustomerInfo";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@id", SqlDbType.NVarChar);
            Sqlparam.Value = session;
            Sqlcomm.Parameters.Add(Sqlparam);


            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }
        public DataTable SelectUser(int id)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_GetByUser";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@id", SqlDbType.Int);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);


            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }
        public cmsDonHangDO SelectBySessionID(string id,int xl)
        {
            cmsDonHangDO objcmsDonHangDO = new cmsDonHangDO();

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsDonHang_GetBySession";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@id", SqlDbType.NVarChar);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@xl", SqlDbType.Int);
            Sqlparam.Value = xl;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["DonHangID"]))
                    objcmsDonHangDO.DonHangID = Convert.ToInt64(dr["DonHangID"]);
                if (!Convert.IsDBNull(dr["HoTen"]))
                    objcmsDonHangDO.HoTen = Convert.ToString(dr["HoTen"]);
                if (!Convert.IsDBNull(dr["DiaChi"]))
                    objcmsDonHangDO.DiaChi = Convert.ToString(dr["DiaChi"]);
                if (!Convert.IsDBNull(dr["SDT"]))
                    objcmsDonHangDO.SDT = Convert.ToString(dr["SDT"]);
                if (!Convert.IsDBNull(dr["Email"]))
                    objcmsDonHangDO.Email = Convert.ToString(dr["Email"]);
                if (!Convert.IsDBNull(dr["HTTT"]))
                    objcmsDonHangDO.HTTT = Convert.ToString(dr["HTTT"]);
                if (!Convert.IsDBNull(dr["DiaChiGiaoHang"]))
                    objcmsDonHangDO.DiaChiGiaoHang = Convert.ToString(dr["DiaChiGiaoHang"]);
                if (!Convert.IsDBNull(dr["TongTien"]))
                    objcmsDonHangDO.TongTien = Convert.ToDouble(dr["TongTien"]);
                if (!Convert.IsDBNull(dr["TienTe"]))
                    objcmsDonHangDO.TienTe = Convert.ToString(dr["TienTe"]);
                if (!Convert.IsDBNull(dr["XuLy"]))
                    objcmsDonHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);
                if (!Convert.IsDBNull(dr["GhiChu"]))
                    objcmsDonHangDO.GhiChu = Convert.ToString(dr["GhiChu"]);
                if (!Convert.IsDBNull(dr["SessionID"]))
                    objcmsDonHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                if (!Convert.IsDBNull(dr["UserID"]))
                    objcmsDonHangDO.UserID = Convert.ToInt64(dr["UserID"]);

            }
            return objcmsDonHangDO;
        }

        #endregion

    }

}
